package com.android.infantschool.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.infantschool.R;
import com.android.infantschool.bean.BabyClassPhotoBean;
import com.android.infantschool.bean.BaseBean;
import com.android.infantschool.bean.JobBean;
import com.android.infantschool.model.config.AppConfig;
import com.android.infantschool.model.config.AppDataSource;
import com.android.infantschool.model.mvp.presenter.BabyClassPhotoPresenter;
import com.android.infantschool.model.mvp.presenter.BabyClassPhotoPresenterImpl;
import com.android.infantschool.model.mvp.view.BabyClassPhotoView;
import com.android.infantschool.ui.adapter.BabyClassPhotoAdapter;
import com.android.infantschool.util.ToastUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author Administrator
 * @particulars
 * @time 2019\6\20 0020 15:32
 * @class describe
 */
public class BabyClassPhotoActivity extends BaseActivity implements BabyClassPhotoAdapter.OnLister, OnRefreshListener, OnLoadMoreListener, BabyClassPhotoView {

    @BindView(R.id.img_back)
    public ImageView imgBack;
    @BindView(R.id.tv_title)
    public TextView tvTitle;
    @BindView(R.id.title)
    public RelativeLayout rlTitle;
    @BindView(R.id.recyclerView)
    public RecyclerView recyclerView;
    @BindView(R.id.tv_img)
    public TextView tvImg;
    @BindView(R.id.view_img)
    public View viewImg;
    @BindView(R.id.tv_video)
    public TextView tvVideo;
    @BindView(R.id.view_video)
    public View viewVideo;
    @BindView(R.id.refreshLayout)
    public SmartRefreshLayout refreshLayout;

    private BabyClassPhotoAdapter adapter = null;
    private BabyClassPhotoPresenter photoPresenter = null;
    private int TYPE_PHONE = 1;//1:图片,2:视频
    private int pageNum = 0;//总页数
    private int pageNow = 1;//当前请求的第几页
    private boolean isRefresh = false;//false:刷新  true:加载

    public static Intent newIntent(Activity activity) {
        Intent intent = new Intent(activity, BabyClassPhotoActivity.class);
        return intent;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_class_photo);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        photoPresenter = new BabyClassPhotoPresenterImpl(this);
        rlTitle.setPadding(0, this.height, 0, 0);
        tvTitle.setText("班级相册");

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new BabyClassPhotoAdapter(this, this);
        recyclerView.setAdapter(adapter);

        //设置 Footer 为 球脉冲 样式
        refreshLayout.setRefreshHeader(new ClassicsHeader(this));
        refreshLayout.setRefreshFooter(new ClassicsFooter(this));
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnLoadMoreListener(this);

        showImg();
    }

    @OnClick({R.id.img_back, R.id.tv_img, R.id.tv_video})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                this.finish();
                break;
            case R.id.tv_img:
                showImg();
                break;
            case R.id.tv_video:
                showVideo();
                break;
        }
    }

    private void showImg() {
        tvImg.setTextColor(0xFF007C70);
        tvVideo.setTextColor(0xFF000000);
        viewImg.setVisibility(View.VISIBLE);
        viewVideo.setVisibility(View.GONE);
        this.showLoading();
        adapter.clear();
        isRefresh = false;
        TYPE_PHONE = 1;
        pageNow = 1;
        getPhotoData(String.valueOf(TYPE_PHONE), String.valueOf(pageNow));
    }

    private void showVideo() {
        tvVideo.setTextColor(0xFF007C70);
        tvImg.setTextColor(0xFF000000);
        viewVideo.setVisibility(View.VISIBLE);
        viewImg.setVisibility(View.GONE);
        this.showLoading();
        adapter.clear();
        isRefresh = false;
        TYPE_PHONE = 2;
        pageNow = 1;
        getPhotoData(String.valueOf(TYPE_PHONE), String.valueOf(pageNow));
    }

    private void getPhotoData(String type, String page) {
        Map<String, String> map = new HashMap<>();
        map.put("token", AppConfig.getToken());
        map.put("type", type);
        map.put("page", page);
        map.put("limit", "5");
        Log.e(TAG, "map:" + map.toString());
        photoPresenter.getBabyClassPhoto(map);
    }

    @Override
    public void lister(int position1, int position2, int type) {
        if (type == 1) {//图片
            List<String> list = new ArrayList<>();
            BabyClassPhotoBean.DataBean bean = adapter.list.get(position1);
            for (BabyClassPhotoBean.DataBean.PhotoURLBean photoURLBean : bean.getPhotoURL()) {
                list.add(photoURLBean.getPath());
            }
            startActivity(ImageActivity.newIntent(this, position2, list));
        }
        if (type == 2) {
            BabyClassPhotoBean.DataBean.PhotoURLBean object = adapter.list.get(position1).getPhotoURL().get(position2);
            startActivity(VideoActivity.newIntent(this, object.getPath()));
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (photoPresenter != null) {
            photoPresenter.onDestroy();
            photoPresenter = null;
        }
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        isRefresh = false;
        pageNow = 1;
        getPhotoData(String.valueOf(TYPE_PHONE), String.valueOf(pageNow));
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        isRefresh = true;
        pageNow++;
        if (pageNow > pageNum) {//没有最新数据
            refreshLayout.finishLoadMore(true);
            refreshLayout.finishRefresh(true);
            return;
        }
        getPhotoData(String.valueOf(TYPE_PHONE), String.valueOf(pageNow));
    }

    @Override
    public void errorBabyClassPhoto(String error) {
        this.dismissLoading();
        refreshLayout.finishLoadMore(true);
        refreshLayout.finishRefresh(true);
        ToastUtil.showShortToast(this, "网络异常:" + error);
    }

    @Override
    public void successBabyClassPhoto(BaseBean baseBean) {
        this.dismissLoading();
        refreshLayout.finishLoadMore(true);
        refreshLayout.finishRefresh(true);
        BabyClassPhotoBean bean = (BabyClassPhotoBean) baseBean;
        if (bean.getCode() == 0) {
            pageNum = bean.getCount() / 5 + 1;//总条数
            if (isRefresh) {//加载
                adapter.addDataAll(bean.getData());
            } else {//刷新
                adapter.setData(bean.getData(), TYPE_PHONE);
            }
            Log.e(TAG, "size:" + adapter.list.size());
        } else {
            ToastUtil.showShortToast(this, bean.getMsg());
        }
    }
}